package br.com.managerprojectplugin.editor;

import java.util.ArrayList;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorPart;

import br.com.managerprojectplugin.model.Stakeholder;

/**
 * Class: StakeHoldersEditor
 * 
 * @author Grupo e-Gen
 * @version 1.0 (07/07/2012)
 * @since 1.0 (14/07/2012)
 */
public class StakeHoldersEditor extends EditorPart {

	public static final String STAKEHOLDERS_ID = "StakeHoldersEditor"; //$NON-NLS-1$
	private TableViewer tableViewer;
	private ArrayList<Stakeholder> stkhldrs = null;

	public StakeHoldersEditor() {
	}

	@Override
	public void setFocus() {
		tableViewer.getControl().setFocus();
	}

	@Override
	public void doSave(IProgressMonitor monitor) {

	}

	@Override
	public void doSaveAs() {

	}

	@Override
	public void init(IEditorSite site, IEditorInput input) {
		setSite(site);
		setInput(input);
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		// Inicializando as variaveis
		stkhldrs = new ArrayList<Stakeholder>();
		for (int i = 0; i < 5; i++) {
			stkhldrs.add(new Stakeholder());
			stkhldrs.get(i).setNome(Messages.StakeHoldersEditor_name + i);
			stkhldrs.get(i).setTelefone("8388380" + i); //$NON-NLS-1$
			stkhldrs.get(i).setEmail("carolinaaa" + i + "@bol.com"); //$NON-NLS-1$ //$NON-NLS-2$
		}

		// Criando a table
		tableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL);

		// Criando as colunas
		TableViewerColumn skNameColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		skNameColumn.setEditingSupport(new EditingSupport(skNameColumn
				.getViewer()) {
			@Override
			protected void setValue(Object element, Object value) {
				if (element instanceof Stakeholder) {
					Stakeholder stakeholder = (Stakeholder) element;
					stakeholder.setNome((String) value);
					tableViewer.refresh(stakeholder);
				}
			}

			@Override
			protected Object getValue(Object element) {
				if (element instanceof Stakeholder) {
					Stakeholder stakeholder = (Stakeholder) element;
					return stakeholder.getNome();
				}
				return null;
			}

			@Override
			protected CellEditor getCellEditor(Object element) {
				if (element instanceof Stakeholder)
					return new TextCellEditor(tableViewer.getTable());
				return null;
			}

			@Override
			protected boolean canEdit(Object element) {
				return true;
			}
		});

		TableViewerColumn skPhoneColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		skPhoneColumn.setEditingSupport(new EditingSupport(skPhoneColumn
				.getViewer()) {

			@Override
			protected void setValue(Object element, Object value) {
				if (element instanceof Stakeholder) {
					Stakeholder stakeholder = (Stakeholder) element;
					stakeholder.setTelefone((String) value);
					tableViewer.refresh(stakeholder);
				}
			}

			@Override
			protected Object getValue(Object element) {
				if (element instanceof Stakeholder) {
					Stakeholder stakeholder = (Stakeholder) element;
					return stakeholder.getTelefone();
				}
				return null;
			}

			@Override
			protected CellEditor getCellEditor(Object element) {
				if (element instanceof Stakeholder) {
					return new TextCellEditor(tableViewer.getTable());
				}
				return null;
			}

			@Override
			protected boolean canEdit(Object element) {
				return true;
			}
		});

		TableViewerColumn skMailColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		skMailColumn.setEditingSupport(new EditingSupport(skMailColumn
				.getViewer()) {

			@Override
			protected void setValue(Object element, Object value) {
				if (element instanceof Stakeholder) {
					Stakeholder stakeholder = (Stakeholder) element;
					stakeholder.setEmail((String) value);
					tableViewer.refresh(stakeholder);
				}
			}

			@Override
			protected Object getValue(Object element) {
				if (element instanceof Stakeholder) {
					Stakeholder stakeholder = (Stakeholder) element;
					return stakeholder.getEmail();
				}
				return null;
			}

			@Override
			protected CellEditor getCellEditor(Object element) {
				if (element instanceof Stakeholder) {
					return new TextCellEditor(tableViewer.getTable());
				}
				return null;
			}

			@Override
			protected boolean canEdit(Object element) {
				return true;
			}
		});

		// tornando a tabela visivel
		tableViewer.getTable().setHeaderVisible(true);
		tableViewer.setContentProvider(new StakeHoldersContentProvider());
		tableViewer.setLabelProvider(new StakeHoldersLabelProvider());

		GridData gd = new GridData();
		gd.horizontalAlignment = SWT.FILL;
		gd.verticalAlignment = SWT.FILL;
		gd.grabExcessVerticalSpace = true;
		gd.grabExcessHorizontalSpace = true;

		tableViewer.setInput(stkhldrs);
		tableViewer.getTable().setLayoutData(gd);

		// dimensionar
		TableLayout tableLayout = new TableLayout();
		tableLayout.addColumnData(new ColumnWeightData(30, 100, true));
		tableLayout.addColumnData(new ColumnWeightData(30, 100, true));
		tableLayout.addColumnData(new ColumnWeightData(20, 100, true));
		tableLayout.addColumnData(new ColumnWeightData(20, 100, true));
		tableViewer.getTable().setLayout(tableLayout);

		Composite composite = new Composite(parent, SWT.NONE);
		Button addSk = new Button(composite, SWT.PUSH);
		addSk.setText(Messages.StakeHoldersEditor_add);
		addSk.setSize(50, 50);

		addSk.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
				stkhldrs.add(new Stakeholder(Messages.StakeHoldersEditor_typeyourname, Messages.StakeHoldersEditor_typeyouremail,
						Messages.StakeHoldersEditor_typeyourfone));
				tableViewer.refresh();
			}
		});

		PlatformUI.getWorkbench().getHelpSystem()
				.setHelp(tableViewer.getControl(), "Manager-projects-plugin"); //$NON-NLS-1$
	}

}
